<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isShow = !isShow"
        >{{ isShow ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channelItem, index) in myChannels"
        :key="index"
        @click="changeChannel(channelItem, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isShow && fixChannel.indexOf(channelItem.id)"
        ></van-icon>
        <span slot="text" class="text" :class="{ active: value === index }"
          >{{ channelItem.name }}
        </span>
      </van-grid-item>
    </van-grid>
    <van-cell title="频道推荐" :border="false"></van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channelItem, index) in recommendChannels"
        :key="index"
        :text="channelItem.name"
        icon="plus"
        @click="addChannel(channelItem)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel.js";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage.js";

export default {
  name: "ChannelEdit",
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      AllChannels: [], //存放所有频道的数组
      isShow: false, //关闭小按钮的状态
      fixChannel: [0], //存放固定频道id的数组
    };
  },
  computed: {
    //所有频道的计算属性
    recommendChannels() {
      return this.AllChannels.filter((channel) => {
        return (
          this.myChannels
            .map((item) => JSON.stringify(item))
            .indexOf(JSON.stringify(channel)) === -1
        );
      });
    },
    // 获取user
    ...mapState(["user"]),
  },
  created() {
    this.loadAllChannels();
  },
  methods: {
    //请求所有频道
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.AllChannels = data.data.channels;
      } catch (err) {
        this.toast("加载失败");
      }
    },
    //所有频道的点击添加
    async addChannel(channel) {
      this.myChannels.push(channel);
      //数据持久化处理
      try {
        if (this.user) {
          // 已登录 把数据存储到后端
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length,
          });
        } else {
          //未登录 把我的频道放到本地
          setItem("TOUTIAO_CHANNELS", this.myChannels);
        }
      } catch (err) {
        this.$toast("添加失败请稍后重试");
      }
    },
    //我的频道的点击删除或者频道切换
    changeChannel(channel, index) {
      if (this.isShow) {
        //是“完成”isShow=true就点击从myChannels中删除
        // 判断要删除的是不是固定频道
        if (this.fixChannel.includes(channel.id)) return;
        // 不是就执行删除操作如果删除的是高亮之后那就没有影响  如果是高亮之前或者高亮 就传高亮-1
        if (index <= this.value) {
          this.$emit("input", this.value - 1);
        }
        this.myChannels.splice(index, 1);
      } else {
        //是“编辑”isShow=false就点击吧当前index传给home实现频道切换
        this.$emit("input", index);
        this.$emit("cutFn");
      }
      this.deleteChannel(channel);
    },
    async deleteChannel(channel) {
      try {
        if (this.user) {
          //已登录 更新到线上
          await deleteUserChannel(channel.id);
        } else {
          //未登录 重新存储myChannels
          setItem("TOUTIAO_CHANNELS", this.myChannels);
        }
      } catch (err) {
        this.$toast("删除失败请稍后重试");
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  //使‘我的频道’居中
  .van-cell__title {
    display: flex;
    align-items: center;
    font-size: 30px;
  }
  .channel-item {
    width: 100px;
    height: 86px;
    // 每个频道的基础样式
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text {
        color: #222;
        font-size: 28px;
      }
    }
  }
  //   频道列表的+号
  /deep/ .recommend-grid {
    .van-grid-item__content {
      flex-direction: row;
      color: #222;
      .van-icon {
        font-size: 24px;
      }
      .van-grid-item__text {
        font-size: 28px;
        margin-top: 0;
      }
    }
  }
  //   我的频道的关闭按钮
  /deep/.van-grid-item__icon-wrapper {
    position: unset;
  }
  /deep/ .van-icon-clear {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
  }

  //  激活高亮
  /deep/ .van-grid-item__content {
    background-color: #f5f5f6;
    .van-grid-item__text,
    .text {
      color: #222;
      font-size: 28px;
      white-space: nowrap;
    }
    .active {
      color: red;
    }
  }
}
</style>